# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer:
pkgname=mingw-w64-gcc
pkgver=11.3.0
pkgrel=0
_islver=0.22
pkgdesc="Cross GCC for the MinGW-w64 cross-compiler"
url="https://gcc.gnu.org"
arch="x86_64 x86"
license="GPL-3.0-or-later"
depends="
	mingw-w64-binutils
	mingw-w64-winpthreads
	mingw-w64-headers
	mingw-w64-crt"
makedepends="zlib-dev mpc1-dev gmp-dev mpfr-dev"
source="
	$pkgname-$pkgver.tar.xz::https://ftp.gnu.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz
	http://libisl.sourceforge.io/isl-$_islver.tar.bz2
	0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
	"
builddir="$srcdir/gcc-$pkgver"

case "$CARCH" in
	x86_64) _target="x86_64-w64-mingw32" ;;
	x86) _target="i686-w64-mingw32" ;;
esac

# Directory for our out-of-tree builds
_gccdir="$srcdir"/gcc-build-$_target

prepare() {
	default_prepare
	mkdir -p "$_gccdir"
}

build() {
	cd "$_gccdir"
	"$builddir"/configure \
		--prefix=/usr \
		--libexecdir=/usr/libexec \
		--target=$_target \
		--enable-languages=c,lto,c++ \
		--enable-shared --enable-static \
		--enable-threads=posix --enable-fully-dynamic-string \
		--enable-libstdcxx-time=yes \
		--with-system-zlib --enable-cloog-backend=isl \
		--enable-lto --disable-dw2-exceptions --enable-libgomp \
		--disable-multilib --enable-checking=release \
		--disable-nls --disable-werror
	make
}

package() {
	cd "$_gccdir"
	make DESTDIR="$pkgdir" install

	$_target-strip "$pkgdir"/usr/$_target/lib/*.dll

	strip "$pkgdir"/usr/bin/$_target-*
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/cc1*
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/collect2
	strip "$pkgdir"/usr/libexec/gcc/$_target/$pkgver/lto*

	ln -s $_target-gcc "$pkgdir"/usr/bin/$_target-cc

	# Move DLLs
	mkdir -p "$pkgdir"/usr/$_target/bin
	mv "$pkgdir"/usr/$_target/lib/*.dll "$pkgdir"/usr/$_target/bin

	strip "$pkgdir"/usr/bin/*

	# Unnecessary files
	rm -rf "$pkgdir"/usr/share
	rm "$pkgdir"/usr/lib/libcc1.*
}

sha512sums="
f0be5ad705c73b84477128a69c047f57dd47002f375eb60e1e842e08cf2009a509e92152bca345823926d550b7395ae6d4de7db51d1ee371c2dc37313881fca7  mingw-w64-gcc-11.3.0.tar.xz
fc2c9796979610dd51143dcefe4f5c989c4354571cc5a1fcc6b932fd41f42a54f6b43adfd289af61be7bd06f3a523fa6a7d7ee56680e32d8036beb4c188fa668  isl-0.22.tar.bz2
8266eae8b3be5557e8cd79c21b6326db7cd83604db6d6ad83923798650fcd8d0f9f1f4ed424c816a48e91f21fb1852ab9272c73100b219f07d3bbb566bfd6fae  0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
"
